package cn.cangnova.cangjie.parser

import cn.cangnova.cangjie.CangJieLanguage
import cn.cangnova.cangjie.lexer.CangJieLexer
import cn.cangnova.cangjie.psi.CangJieElementType
import cn.cangnova.cangjie.psi.CjFile
import cn.cangnova.cangjie.psi.CjFileElementType
import cn.cangnova.cangjie.psi.CjTokens
import com.intellij.lang.ASTNode
import com.intellij.lang.ParserDefinition
import com.intellij.lang.PsiParser
import com.intellij.lexer.Lexer
import com.intellij.openapi.project.Project
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.TokenType
import com.intellij.psi.tree.IFileElementType
import com.intellij.psi.tree.TokenSet

class CangJieParserDefinition : ParserDefinition {
    companion object {
        val FILE = IFileElementType(CangJieLanguage)

        val COMMENTS = TokenSet.create(
            CangJieElementType.Companion.LINE_COMMENT,
            CangJieElementType.Companion.BLOCK_COMMENT
        )

        val WHITE_SPACES = TokenSet.create(
            TokenType.WHITE_SPACE
        )

        val STRING_LITERALS = TokenSet.create(
            // TODO: 添加字符串字面量类型
        )
    }

    override fun createLexer(project: Project?): Lexer = CangJieLexer()

    override fun createParser(project: Project?): PsiParser = CangJieParser()

    override fun getFileNodeType() = CjFileElementType.Companion.INSTANCE

    override fun getCommentTokens(): TokenSet = COMMENTS

    override fun getStringLiteralElements(): TokenSet = STRING_LITERALS

    override fun getWhitespaceTokens(): TokenSet = WHITE_SPACES

    override fun createElement(node: ASTNode): PsiElement {
        return CjTokens.Factory.createElement(node)
    }

    override fun createFile(viewProvider: FileViewProvider): PsiFile {
        return CjFile(viewProvider)
    }
}